package org.yn.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.sys.service.IConfigService;
import org.yn.sys.domain.Config;
import org.yn.sys.query.ConfigQuery;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@PermissionAnnotation(name="系统管理",descs = "系统管理api")
@Api("系统管理接口")
@RestController
@RequestMapping("/config")
public class ConfigController {
    @Autowired
    public IConfigService configService;


    /**
     * 保存和修改公用的
     * @param config  传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name="查询所有配置",descs = "查询所有配置")
    @ApiOperation(value = "查询所有配置")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody Config config){
        try {
            if( config.getId()!=null)
                configService.update(config);
            else
                configService.insert(config);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name="删除配置",descs = "删除配置")
    @ApiOperation(value = "删除配置")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            configService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @PermissionAnnotation(name="根据id获取配置",descs = "根据id获取配置")
    @ApiOperation(value = "根据id获取配置")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id)
    {
        try {
            Config config = configService.selectById(id);
            return AjaxResult.success(config);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @PermissionAnnotation(name="获取全部配置",descs = "获取全部配置")
    @ApiOperation(value = "获取全部配置")
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Config> list = configService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name="分页查询配置",descs = "分页查询配置")
    @ApiOperation(value = "分页查询配置")
    @PutMapping
    public AjaxResult queryPage(@RequestBody ConfigQuery query)
    {
        try {
            PageInfo<Config> pageInfo = configService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量查询配置",descs = "批量查询配置")
    @ApiOperation(value = "批量查询配置")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                configService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }
}
